﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Share_PersonDetails : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {   
        loadPerson();
        SetRoleView();
    }
    public void loadPerson()
    {
        if (Request["UserID"] != null)
        {
            
         //   Session["Details"] = Request["UserID"].ToString();
            string UserID = Request["UserID"].ToString();
            
            UserDAO userDAO = new UserDAO();
            UserInfo userInfo = new UserInfo();
            userInfo = userDAO.GetByID(UserID);
            IList<UserInfo> list = new List<UserInfo>();
            list.Add(userInfo);            
            dtl_PersonDetails.DataSource = list;
            dtl_PersonDetails.DataBind();
            
            string typeofAccount = Request["TypeofAccount"].ToString();
            RoleDAO rolesDAO = new RoleDAO();
            string roleName = rolesDAO.GetRoles(UserID);
            string TypeOfId = userDAO.GetTypeByID(UserID);
            Label1.Text = TypeOfId;
            if (TypeOfId.Equals("Admin") || TypeOfId.Equals("Manager"))

          
            {
                foreach (DataListItem item in dtl_PersonDetails.Items)
                {
                    try
                    {
                        ((Label)item.FindControl("lb_DateJoin")).Visible = true;
                        ((Label)item.FindControl("lb_DateJoin_Data")).Visible = true;  
                    }catch(Exception e)
                    {
                    }
                    
                }
            }
            else if (TypeOfId.Equals("Staff"))
            {
                foreach (DataListItem item in dtl_PersonDetails.Items)
                {
                    try
                    {
                        ((Label)item.FindControl("lb_Class")).Visible = true;
                        ((Label)item.FindControl("lb_Class_Data")).Visible = true;
                        ((Label)item.FindControl("lb_DateJoin")).Visible = true;
                        ((Label)item.FindControl("lb_DateJoin_Data")).Visible = true;
                        ((Label)item.FindControl("lb_Subject")).Visible = true;
                        ((Label)item.FindControl("lb_Subject_Data")).Visible = true;
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            else if (TypeOfId.Equals("Student"))
            {
                foreach (DataListItem item in dtl_PersonDetails.Items)
                {
                    try
                    {
                        ((Label)item.FindControl("lb_Class")).Visible = true;
                        ((Label)item.FindControl("lb_Class_Data")).Visible = true;
                        ((Label)item.FindControl("lb_Course")).Visible = true;
                        ((Label)item.FindControl("lb_Course_Data")).Visible = true;
                        ((Label)item.FindControl("lb_DateJoin")).Visible = true;
                        ((Label)item.FindControl("lb_DateJoin_Data")).Visible = true;
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
        }

    }

    public void loadEdit()
    {
        if (Request["UserID"] != null)
        {
            string userID = Request["UserID"].ToString();
            UserDAO userDAO = new UserDAO();
            UserInfo userInfo = new UserInfo();
            userInfo = userDAO.GetByID(userID);
            IList<UserInfo> list = new List<UserInfo>();
            list.Add(userInfo);
            dtl_PersonDetails.DataSource = list;
            dtl_PersonDetails.DataBind();

            string typeofAccount = Request["TypeofAccount"].ToString();
            RoleDAO rolesDAO = new RoleDAO();
            string roleName = rolesDAO.GetRoles(userID);
            string TypeOfId = userDAO.GetTypeByID(userID);

            if (TypeOfId.Equals("Admin") || TypeOfId.Equals("Manager"))
            {

                foreach (DataListItem item in dtl_PersonDetails.Items)
                {
                    try
                    {
                        ((Label)item.FindControl("lb_DateJoin")).Visible = true;
                        ((TextBox)item.FindControl("txt_DateJoin_Data")).Visible = true;

                        ((RequiredFieldValidator)item.FindControl("rqrPhone")).Visible = false;
                        ((RequiredFieldValidator)item.FindControl("rqrClass")).Visible = false;
                        ((RequiredFieldValidator)item.FindControl("rqrCourse")).Visible = false;
                        ((RequiredFieldValidator)item.FindControl("rqrSubject")).Visible = false;
                    }
                    catch (Exception e)
                    {
                    }

                }
            }
            else if (TypeOfId.Equals("Staff"))
            {
                foreach (DataListItem item in dtl_PersonDetails.Items)
                {
                    try
                    {
                        ((Label)item.FindControl("lb_Class")).Visible = true;
                        ((TextBox)item.FindControl("txt_Class_Data")).Visible = true;

                        ((Label)item.FindControl("lb_Subject")).Visible = true;
                        ((TextBox)item.FindControl("txt_Subject_Data")).Visible = true;

                        ((Label)item.FindControl("lb_DateJoin")).Visible = true;
                        ((TextBox)item.FindControl("txt_DateJoin_Data")).Visible = true;

                        ((RequiredFieldValidator)item.FindControl("rqrPhone")).Visible = false;
                        ((RequiredFieldValidator)item.FindControl("rqrCourse")).Visible = false;

                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            else if (TypeOfId.Equals("Student"))
            {
                foreach (DataListItem item in dtl_PersonDetails.Items)
                {
                    try
                    {
                        ((Label)item.FindControl("lb_Class")).Visible = true;
                        ((TextBox)item.FindControl("txt_Class_Data")).Visible = true;

                        ((Label)item.FindControl("lb_Course")).Visible = true;
                        ((TextBox)item.FindControl("txt_Course_Data")).Visible = true;

                        ((Label)item.FindControl("lb_DateJoin")).Visible = true;
                        ((TextBox)item.FindControl("txt_DateJoin_Data")).Visible = true;

                        ((RequiredFieldValidator)item.FindControl("rqrPhone")).Visible = false;
                        ((RequiredFieldValidator)item.FindControl("rqrEmail")).Visible = false;
                        ((RequiredFieldValidator)item.FindControl("rqrSubject")).Visible = false;
                    }
                    catch (Exception e)
                    {
                    }
                }
            }
            else if (TypeOfId.Equals("Customer"))
            {
                foreach (DataListItem item in dtl_PersonDetails.Items)
                {
                    try
                    {
                        ((TextBox)item.FindControl("txt_DateJoin_Data")).Visible= false;
                        ((RequiredFieldValidator)item.FindControl("rqrEmail")).Visible = false;
                        ((RequiredFieldValidator)item.FindControl("rqrBirthday")).Visible = false;
                        ((RequiredFieldValidator)item.FindControl("rqrGender")).Visible = false;
                    }
                    catch (Exception e) { }
                }
            }



        }

    }

    public void loadFormInsert()
    {
        
    }
    protected void dtl_PersonDetails_EditCommand(object source, DataListCommandEventArgs e)
    {
        dtl_PersonDetails.EditItemIndex = e.Item.ItemIndex;        
            loadEdit();
    }
    public void Hidedetail(object sender, EventArgs e)
    {
        string[] temp = Request.Url.ToString().Split('?');

        Response.Redirect(temp[0]);
    }

    protected void dtl_PersonDetails_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        UserInfo userInfo = new UserInfo();
        UserDAO userDAO = new UserDAO();

       
             userInfo.UserID = ((Label)e.Item.FindControl("lb_UserID")).Text;
             userInfo.FullName = ((TextBox)e.Item.FindControl("txtFullName")).Text;
             //userInfo.BirthDay = DateTime.Parse(((TextBox)e.Item.FindControl("txtBirthday")).Text);
             userInfo.BirthDay = ((TextBox)e.Item.FindControl("txtBirthday")).Text;
             userInfo.Gender = ((TextBox)e.Item.FindControl("txtGender")).Text;
             userInfo.Email = ((TextBox)e.Item.FindControl("txtEmail")).Text;
             userInfo.Address = ((TextBox)e.Item.FindControl("txtAddress")).Text;
             userInfo.Phone = ((TextBox)e.Item.FindControl("txtPhone")).Text;
             userInfo.DateJoin = ((TextBox)e.Item.FindControl("txt_DateJoin_Data")).Text;             
             userInfo.St_class = ((TextBox)e.Item.FindControl("txt_Class_Data")).Text;
             userInfo.Course = ((TextBox)e.Item.FindControl("txt_Course_Data")).Text;
             userInfo.Subject = ((TextBox)e.Item.FindControl("txt_Subject_Data")).Text;


                if (userDAO.Update(userInfo) > 0)
                {
                 //   ((Label)e.Item.FindControl("lblResult")).Text = "Update Sucessfully!";
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Update Successfull!');", true);
                    dtl_PersonDetails.EditItemIndex = -1;
                    loadPerson();
                }
                else
                {
                    ((Label)e.Item.FindControl("lblResult")).Text = "Update Failed!";
                }


                
    
    }
    public void SetRoleView()
    {
        if (Session["roleID"] != null)
        {
            int roleID = int.Parse(Session["roleID"].ToString());
            if (roleID == 2 || roleID == 3)
            {
               
                foreach (DataListItem item in dtl_PersonDetails.Items)
                {
                    ((Button)item.FindControl("btnUpdate")).Visible= false;
                    
                    
                }
                
            }
        }
    }
    protected void dtl_PersonDetails_CancelCommand(object source, DataListCommandEventArgs e)
    {
        dtl_PersonDetails.EditItemIndex = -1;
        loadEdit();
        loadPerson();
    }
}